package com.zara.store.main;

import javax.naming.NamingException;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

import com.zara.store.client.clientmodel.EnvtModel;
import com.zara.store.client.clientmodel.OfadModel;
import com.zara.store.client.clientmodel.PalcModel;
import com.zara.store.client.clientmodel.RetailModel;
import com.zara.store.client.model.StoreProxy;
import com.zara.store.client.view.EnvtView;
import com.zara.store.client.view.OfadView;
import com.zara.store.client.view.PalcView;
import com.zara.store.client.view.RetailView;
import com.zara.store.client.view.swing.MenuForm;

import framework.clientmodel.Model;

public class Main {

	private Model retailModel;
	private Model palcModel;
	private Model envtModel;
	private Model ofadModel;

	public Main(StoreProxy sp) throws NamingException {
		retailModel = new RetailModel(sp);
		palcModel = new PalcModel(sp);
		envtModel = new EnvtModel(sp);
		ofadModel = new OfadModel(sp);
	}
	
	private static JWindow splash;
	
	public static void main (String args[]) throws NamingException {
        SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				splash = new JWindow();
				splash.getContentPane().add(new JLabel(createImageIcon("/com/zara/store/client/resources/zara.jpg")));
				splash.pack();
				splash.setLocationRelativeTo(splash.getOwner());
				splash.setVisible(true);
			}
        });
		final StoreProxy storeProxy = new StoreProxy();
		final Main admin = new Main(storeProxy);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                //UIManager.put("swing.boldMetal", Boolean.FALSE); 
        		LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
        		try {
        			UIManager.setLookAndFeel(infos[2].getClassName());
        		} catch (Exception e) {
        			e.printStackTrace();
        		}
        		admin.init();
        		splash.setVisible(false);
            }
        });
	}

	protected void init() {
		RetailView retailView = new RetailView(retailModel);
		PalcView palcView = new PalcView(palcModel);
		EnvtView envtView = new EnvtView(envtModel);
		OfadView ofadView = new OfadView(ofadModel);
		new MenuForm(retailView, palcView, envtView, ofadView);
	} 
	 
    public static ImageIcon createImageIcon(final String path) {
        final java.net.URL imgURL = Main.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            return null;
        }
    }
}
